﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

/*
 * 1.在场人员实时统计：总人数，管理人员，外委人员，施工人员
 * 2.各车间总人数
 * 3.历史在场人员统计：总人数，管理人员，外委人员，施工人员
 */
public class StaffUI : MonoBehaviour
{
    public static StaffUI instances;
    public TextMeshProUGUI builderUGUI;
    public TextMeshProUGUI managerUGUI;
    public TextMeshProUGUI outUGUI;
    public TextMeshProUGUI allUGUI;

    private static int builderSta;//施工人员
    private static int managerSta;//管理人员
    private static int outSta;//外来人员

    public StaffUIItem staffItem;//人员UI
    public ScrollRect itemParent;
    public List<StaffUIItem> allItemID_List;
    void Start()
    {

    }

    //获取各类人员总人数
    public void GetAllStaff()
    {
        builderSta = 0;
        managerSta = 0;
        outSta = 0;
        for (int i = 0; i < Main.instance.workShop_List.Count; i++)
        {
            //可获取不同车间的人员数量

            GetRole(Main.instance.workShop_List[i].buildInsidersReply.data.content);
            outSta += Main.instance.workShop_List[i].outSideInsidersReply.data.Count;
        }
        //展示具体数量
        DOTween.To((value) => { builderUGUI.text = ((int)value).ToString(); }, 0, builderSta, 1.3f);
        DOTween.To((value) => { managerUGUI.text = ((int)value).ToString(); }, 0, managerSta, 1.3f);
        DOTween.To((value) => { outUGUI.text = ((int)value).ToString(); }, 0, outSta, 1.3f);
        DOTween.To((value) => { allUGUI.text = ((int)value).ToString(); }, 0, outSta + builderSta + managerSta, 1.3f);
    }

    //获取车间内部人员（施工，管理）数量
    public void GetRole(List<BuildInsidersDataItem> buildInsidersDatas)
    {
        for (int i = 0; i < buildInsidersDatas.Count; i++)
        {
            if (buildInsidersDatas[i].role == "0")//施工人员
            {
                builderSta++;
            }
            else
            {
                managerSta++;
            }
            //AddStaffItem();
        }
    }

    [ContextMenu("testTo")]
    public void testTo()
    {
        DOTween.To((value) => { builderUGUI.text = ((int)value).ToString(); }, 0, 20, 1f);
        DOTween.To((value) => { Debug.Log((int)value); }, 0, 20, 1.5f);
    }

    public void AddStaffItem(string id, string name, string postName)
    {
        int index = allItemID_List.FindIndex((a) => (a.id == id));
        if (index != 1)
        {
            allItemID_List[index].change_bool = true;
        }
        else //若不存在则创建
        {
            StaffUIItem staffUIItem = Instantiate<StaffUIItem>(staffItem,itemParent.content);
            staffUIItem.id = id;
            staffUIItem._name = name;
            staffUIItem.line = true;
            staffUIItem.change_bool = true;
            allItemID_List.Add(staffUIItem);
        }
    }
}
